//
//  MTAuthenticationController.swift
//  MT
//
//  Created by 陈悦 on 2020/5/29.
//  Copyright © 2020 张强. All rights reserved.
//

import Moya
import Result
import RxSwift
import RxCocoa
import MTCategory

class MTAuthenticationController: BaseViewController {
    
    /// MTProvider
    private let disposeBag = DisposeBag()
    
    private lazy var user_provider: MTProvider<MineApi> = {
        return MTProvider<MineApi>(plugins: [])
    }()
    
    
    private var model: MTCerSummarySubModel?
    
    var minemodel: MineInfoModel?
    
    //商家
    lazy var businessView: UIView = {
        var view = UIView()
        view.backgroundColor = UIColor.theme.background
        view.layer.cornerRadius = 8
        let tap = UITapGestureRecognizer(target: self, action: #selector(authenticationController(didClickCompanyButton:)))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    lazy var businessIcon: UIImageView = {
        var image = UIImageView()
        image.image = UIImage(named: "company")
        image.contentMode = .scaleAspectFill
        return image
    }()
    
    lazy var businessLab: UILabel = {
        var label = UILabel()
        label.text = "商家认证"
        label.textColor = UIColor.text.x1d1a1a
        label.font = PingFangSCSemiboldFont(14)
        label.textAlignment = .center
        return label
    }()
    
    lazy var businessButton: UIButton = {
        var button = UIButton()
        button.setTitle("未认证", for: .normal)
        button.setTitleColor(UIColor.text.x1d1a1a, for: .normal)
        button.titleLabel?.font = PingFangSCSemiboldFont(12)
        button.backgroundColor = UIColor.theme.main
        button.layer.cornerRadius=12
        button.isUserInteractionEnabled = false
        //button.addTarget(self, action: #selector(authenticationController(didClickCompanyButton:)), for: .touchUpInside)
        return button
    }()
    
    //个人
    lazy var personalView: UIView = {
        var view = UIView()
        view.backgroundColor = UIColor.theme.background
        view.layer.cornerRadius = 8
        let tap = UITapGestureRecognizer(target: self, action: #selector(authenticationController(didClickPersonalButton:)))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    lazy var personalIcon: UIImageView = {
        var image = UIImageView()
        image.image = UIImage(named: "personal")
        image.contentMode = .scaleAspectFill
        return image
    }()
    
    lazy var personalLab: UILabel = {
        var label = UILabel()
        label.text = "个人认证"
        label.textColor = UIColor.text.x1d1a1a
        label.font = PingFangSCSemiboldFont(14)
        label.textAlignment = .center
        return label
    }()
    
    lazy var personalButton: UIButton = {
        var button = UIButton()
        button.setTitle("未认证", for: .normal)
        button.setTitleColor(UIColor.text.x1d1a1a, for: .normal)
        button.titleLabel?.font = PingFangSCSemiboldFont(12)
        button.backgroundColor = UIColor.theme.main
        button.isUserInteractionEnabled = false
        button.layer.cornerRadius=12
        //button.addTarget(self, action: #selector(authenticationController(didClickPersonalButton:)), for: .touchUpInside)
        return button
    }()

}

// MARK: ------------------------------------ Overrides ----------------------------------------
extension MTAuthenticationController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initializeUI()
        loadData()
    }
}

// MARK: ---------------------------------------- 初始化 --------------------------------------------
extension MTAuthenticationController {
    
    
    private func initializeUI() {
        view.backgroundColor = UIColor(hex: 0xf6f6f6)
        navigationItem.title = "添加认证"
        
        view.addSubview(businessView)
        businessView.addSubview(businessIcon)
        businessView.addSubview(businessLab)
        businessView.addSubview(businessButton)
        view.addSubview(personalView)
        personalView.addSubview(personalIcon)
        personalView.addSubview(personalLab)
        personalView.addSubview(personalButton)
        
        businessView.snp.makeConstraints { (make) in
            make.top.equalTo(20+NavigationBarHeight+StatusBarHeight)
            make.left.equalTo(10)
            make.width.equalTo((ScreenWidth - 30) / 2)
            make.height.equalTo(187)
        }
        
        businessIcon.snp.makeConstraints { (make) in
            make.top.equalTo(25)
            make.width.height.equalTo(64)
            make.centerX.equalTo(businessView)
        }
        
        businessLab.snp.makeConstraints { (make) in
            make.top.equalTo(businessIcon.snp.bottom).offset(10)
            make.width.equalTo(businessView)
        }
        
        businessButton.snp.makeConstraints { (make) in
            make.top.equalTo(businessLab.snp.bottom).offset(20)
            make.width.equalTo(80)
            make.height.equalTo(24)
            make.centerX.equalTo(businessView)
        }
        
        personalView.snp.makeConstraints { (make) in
            make.top.equalTo(20+NavigationBarHeight+StatusBarHeight)
            make.right.equalTo(-10)
            make.width.equalTo((ScreenWidth - 30) / 2)
            make.height.equalTo(187)
        }
        
        personalIcon.snp.makeConstraints { (make) in
            make.top.equalTo(25)
            make.width.height.equalTo(64)
            make.centerX.equalTo(personalView)
        }
        
        personalLab.snp.makeConstraints { (make) in
            make.top.equalTo(personalIcon.snp.bottom).offset(10)
            make.width.equalTo(personalView)
        }
        
        personalButton.snp.makeConstraints { (make) in
            make.top.equalTo(personalLab.snp.bottom).offset(20)
            make.width.equalTo(80)
            make.height.equalTo(24)
            make.centerX.equalTo(personalView)
        }
        
        guard let model = minemodel else { return }
        if (model.certificationType == 1 && (model.certificationStatus == 0 || model.certificationStatus == 1)){
            ///审核中,已认证不显示另一种认证方式
            businessView.isHidden = true
            personalView.isHidden = false
            personalView.snp.remakeConstraints { (make) in
                make.top.equalTo(20+NavigationBarHeight+StatusBarHeight)
                make.left.equalTo(10)
                make.width.equalTo(172)
                make.height.equalTo(187)
            }
        } else if model.certificationStatus == 2 {
        //else if (model.certificationType == 1 && model.certificationStatus == 2) || (
            //model.certificationType == 2 && model.certificationStatus == 2){
            ///认证失败可以提交另一种
            businessView.isHidden = false
            personalView.isHidden = false
        } else if (model.certificationType == 2 && (model.certificationStatus == 0 || model.certificationStatus == 1)) {
            businessView.isHidden = false
            personalView.isHidden = true
            businessView.snp.remakeConstraints { (make) in
                make.top.equalTo(20+NavigationBarHeight+StatusBarHeight)
                make.left.equalTo(10)
                make.width.equalTo(172)
                make.height.equalTo(187)
            }
        } else if model.certificationStatus == 0 {
            /// 认证中, 则
            if model.certificationType == 1 {
                businessView.isHidden = true
                personalView.isHidden = false
            } else if model.certificationType == 2 {
                businessView.isHidden = false
                personalView.isHidden = true
            } else {
                businessView.isHidden = false
                personalView.isHidden = false
            }
        } else {
            businessView.isHidden = false
            personalView.isHidden = false
        }
        
        switch model.certificationStatus {
        case -1:
            businessButton.setTitle("未认证", for: .normal)
            personalButton.setTitle("未认证", for: .normal)
            businessButton.backgroundColor = UIColor.theme.main
            personalButton.backgroundColor = UIColor.theme.main
            personalButton.setTitleColor(UIColor.text.x1d1a1a, for: .normal)
            businessButton.setTitleColor(UIColor.text.x1d1a1a, for: .normal)
            break
        case 0:
            businessButton.setTitle("审核中", for: .normal)
            personalButton.setTitle("审核中", for: .normal)
            businessButton.backgroundColor = UIColor.theme.background
            personalButton.backgroundColor = UIColor.theme.background
            personalButton.setTitleColor(UIColor(hex: 0xff003d), for: .normal)
            businessButton.setTitleColor(UIColor(hex: 0xff003d), for: .normal)
            break
        case 1:
            if model.certificationType == 1 {
                personalButton.setTitle("已认证", for: .normal)
                personalButton.setTitleColor(UIColor.text.x1d1a1a, for: .normal)
                personalButton.backgroundColor = UIColor.theme.background
                personalButton.layer.borderColor = UIColor.theme.main.cgColor
                personalButton.layer.borderWidth = 1
                personalButton.setImage(UIImage(named: "person-cer"), for: .normal)
                personalButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: -40, bottom: 0, right: 0)
                personalButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 0)
            } else {
                businessButton.setTitle("已认证", for: .normal)
                businessButton.setTitleColor(UIColor.text.x1d1a1a, for: .normal)
                businessButton.backgroundColor = UIColor.theme.background
                businessButton.layer.borderColor = UIColor.theme.main.cgColor
                businessButton.layer.borderWidth = 1
                businessButton.setImage(UIImage(named: "company-cer"), for: .normal)
                businessButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: -40, bottom: 0, right: 0)
                businessButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 0)
            }
            break
        case 2:
            businessButton.setTitle(model.certificationType == 2 ? "重新认证" : "未认证", for: .normal)
            personalButton.setTitle(model.certificationType == 1 ? "重新认证" : "未认证", for: .normal)
            if model.certificationType == 1 {
                businessButton.backgroundColor = UIColor.theme.main
                businessButton.setTitleColor(UIColor.text.x1d1a1a, for: .normal)
                personalButton.backgroundColor = UIColor(hex:0xff003d)
                personalButton.setTitleColor(UIColor.theme.background, for: .normal)
            } else {
                personalButton.backgroundColor = UIColor.theme.main
                personalButton.setTitleColor(UIColor.text.x1d1a1a, for: .normal)
                businessButton.backgroundColor = UIColor(hex:0xff003d)
                businessButton.setTitleColor(UIColor.theme.background, for: .normal)
            }
            break
        default:
            break
        }
    }
}

// MARK: ---------------------------------------- 数据 --------------------------------------------
extension MTAuthenticationController {
    private func loadData() {
        user_provider.rx.requestResult(.applySummary).subscribe(onNext: { [weak self] (result: Result<MTCerSummaryResponseModel, AnyError>, isCache: Bool) in
            guard let weakself = self else { return }
            switch result {
            case let .success(response):
                if response.code == 200 {
                    weakself.model = response.result
                } else {
                    AlertManager.presentAlertView(message: response.message ?? "")
                }
                break
                
            case let .failure(error):
                AlertManager.presentAlertView(message: error.localizedDescription)
                break
            }
        }).disposed(by: disposeBag)
    }
    
}

// MARK: ---------------------------------------- 事件方法 --------------------------------------------
@objc
extension MTAuthenticationController {
    
    private func authenticationController(didClickCompanyButton sender: UIButton) {
        if minemodel?.certificationType == 2 && minemodel?.certificationStatus == 0 {
            MBProgressHUD_JChat.show(text: "商家认证认证中, 请耐心等待", view: self.view, 1)
            return
        }
        let vc = MTCompanyAuthController()
        vc.model = model
        navigationController?.pushViewController(vc, animated: true)
    }
    
    private func authenticationController(didClickPersonalButton sender: UIButton) {
        if minemodel?.certificationType == 1 && minemodel?.certificationStatus == 0 {
            MBProgressHUD_JChat.show(text: "个人认证认证中, 请耐心等待", view: self.view, 1)
            return
        }
        let vc = MTPersonAuthController()
        vc.model = model
        navigationController?.pushViewController(vc, animated: true)
    }
    
}

extension MTAuthenticationController {
    private func cerStatus(status: Int) -> String {
        switch status {
        case -1:
            return "未认证"
        case 0:
            return "审核中"
        case 1:
            return "已通过"
        case 2:
            return "已拒绝"
        default:
            return "未认证"
        }
    }

}
